package com.econ.powercloud.ems.util;

import com.google.common.collect.Lists;

import java.util.Collections;
import java.util.List;

/**
 * @Author : zjf
 * @Description :
 * @Date : 2022/11/18 10:03
 * @Modified by : zjf
 */
public class PageUtil {

    public static <T> List<T> getPageSizeDataForRelations(List<T> datas, int pageNo, int pageSize) {
        //起始截取数据位置
        int startNum = (pageNo - 1) * pageSize + 1;
        if (startNum > datas.size()) {
            return Collections.emptyList();
        }
        List<T> res = Lists.newArrayList();
        int rum = datas.size() - startNum;
        if (rum < 0) {
            return Collections.emptyList();
        }
        //说明正好是最后一个了
        if (rum == 0) {
            int index = datas.size() - 1;
            res.add(datas.get(index));
            return res;
        }
        //剩下的数据还够1页，返回整页的数据
        if (rum / pageSize >= 1) {
            //截取从startNum开始的数据
            for (int i = startNum; i < startNum + pageSize; i++) {
                res.add(datas.get(i - 1));
            }
            return res;
        } else if ((rum / pageSize == 0) && rum > 0) {
            //不够一页，直接返回剩下数据
            for (int j = startNum; j <= datas.size(); j++) {
                res.add(datas.get(j - 1));
            }
            return res;
        } else {
            return null;
        }
    }
}
